package day1_12.FirstPhase.StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        ArrayList<Student> student = new ArrayList<>();
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择: ");
            Scanner sc = new Scanner(System.in);
            String choice = sc.next();
            switch (choice) {
                case "1"-> addStudent(student);
                case "2"-> deleteStudent(student);
                case "3"-> updateStudent(student);
                case "4"-> queryStudent(student);
                case "5"-> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void addStudent(ArrayList<Student> student) {
        Student s = new Student();

        System.out.println("添加学生");
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("请输入学生的id");
            String id = sc.next();
            if (containsStudent(student, id)) {
                System.out.println("id已经存在,请重新录入");
            }
            s.setId(id);
            break;
        }

        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭地址");
        String address = sc.next();
        s.setAddress(address);


        student.add(s);
        System.out.println("学生信息添加成功");

    }


    public static void deleteStudent(ArrayList<Student> student) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index = getIndex(student, id);
        if ( index >= 0) {
            student.remove(index);
            System.out.println("id为" + id + "的学生删除成功");
        }else {
            System.out.println("id不存在,删除失败");
        }
    }

    public static void updateStudent(ArrayList<Student> student) {
        System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        System.out.println("请输入要修改的学生的id");
        int index = getIndex(student, id);
        if (index >= 0) {
            Student s = student.get(index);
            System.out.println("请输入新的姓名");
            s.setName(sc.next());

            System.out.println("请输入新的年龄");
            s.setAge(sc.nextInt());

            System.out.println("请输入新的家庭住址");
            s.setAddress(sc.next());

            System.out.println("修改成功");
        }else {
            System.out.println("要修改的id: " + "不存在,请重新输入");
        }
    }
    public static void queryStudent(ArrayList<Student> student) {
        if (student.isEmpty()) {
            System.out.println("当前无学生信息,请添加后再查询");
            return;
        }

        System.out.println("id\t\t\t姓名\t年龄\t家庭住址");

        for (int i = 0; i < student.size(); i++) {
            Student stu = student.get(i);
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
        }
    }

    public static boolean containsStudent(ArrayList<Student> student , String id) {
//        for (int i = 0; i < student.size(); i++) {
//            Student stu = student.get(i);
//            if (stu.getId().equals(id)) {
//                return true;
//            }
//        }
//        return false;
        return getIndex(student,id) >= 0;
    }

    // 通过id获取索引
    public static int getIndex(ArrayList<Student> student, String id) {
        for (int i = 0; i < student.size(); i++) {
            Student stu = student.get(i);
            if (stu.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
